/**
 * Created by Davy on 2017/4/25.
 */
(function ($, window, document, undefined) {
    var $window = $(window);
    $.fn.loading = function (options) {

        var settings = {
            autoClose: false,
            time: 10 * 1000,
            logo: true,
            glass: false,
        };
        if (options && typeof options === 'object') {
            $.extend(settings, options);
        }

        var elements = this,
            logo = settings.logo ? '<div class="loading-logo"></div>' : '',
            $_loading = $('<div class="transparent-base64 loading"><div class="loading-container">' +
                '</div>' + logo + '</div>'),
            $loading = $('.loading'),
            closeHandle = function () {
                $("body").removeClass("loading-open");
                $(this).remove();
                $(".loading-portion").removeClass("loading-portion");

            };

        if (typeof options === 'string' && options === 'close') {
            $loading.fadeOut(closeHandle);
            return elements;
        }
        closeHandle.apply($loading);

        if (!elements.is("body") && elements.css("position") === "static") {
            if (settings.glass) {
                elements.addClass("glass-back");
            }
            elements.addClass("loading-portion");
            $_loading.addClass("loading-portion-content")
        } else {
            $("body").addClass("loading-open");
        }
        var gifHeight = 73, logoHeight = 75;
        var left = elements.width() / 2 - 100;
        var gifLeft = elements.width() / 2 - 50;
        var _height = elements.height();
        var top = elements.height() / 5.5;
        if (_height <= (gifHeight + logoHeight)) {
            $_loading.find(".loading-logo").remove();
        } else {
            $_loading.find(".loading-logo").css({"left": left + "px", top: (top + 100) + "px"});
        }
        $_loading.find(".loading-container").css({"left": gifLeft + "px", top: top + "px"});

        $_loading.appendTo(elements).fadeIn('slow');
        if (settings.autoClose) {
            setTimeout(function () {
                $('.loading').fadeOut(closeHandle);
            }, setTimeout.time);
        }
        return elements;
    }
})(jQuery, window, document);
/*(function ($, window, document, undefined) {
 var $window = $(window);
 // 移动接口统一laoding样式
 $.fn.loading = function() {
 var $this = $(this);
 if($this.length <= 0) {
 return false;
 }
 var _width = $this.outerWidth();
 var _height = $this.outerHeight();
 var _offset = $this.offset();
 var el = '<div class="loading-mask"></div>';
 $('.loading-mask').remove();

 $('body').append(el);
 $('.loading-mask').css({
 'width': _width,
 'height': _height,
 'top': _offset.top,
 'left': _offset.left
 });
 if($(this)[0].nodeName === 'BODY') {
 $('.loading-mask').css({
 'width': '100%',
 'height':'100%',
 'position': 'fixed',
 'z-index': '9999'
 })
 }
 };
})(jQuery, window, document);
 $.loading = function() {
 $('body').loading();
 };*/
// $('.u-main').loading();
// $.loading();